<?php

class sp extends CI_Controller
{
    function index()
    {
        $this->pub->logp('浏览商品', '1-2-3');
        $da = $this->pub->get_setting('商品', 'Commodity');

        $q = $this->db->get_where('sp_brand');
        $da['brand'] = $q->result_array();

        $this->load->view('adminx/sp', $da);
    }

    function add_new()
    {
        $this->load->view('adminx/sp_add_new', $da);
    }

    function add()
    {
        $this->pub->logp('添加商品', '1-2-3');
        $dax['msg'] = '';
        if ($_POST) {
            $da['fl1'] = $this->input->post('fl1');
            $da['brand_id'] = $this->input->post('brand_id');
            $da['sort'] = $this->input->post('sort');
            $da['name'] = $this->input->post('name');

            $da['tj'] = $this->input->post('tj');
            $da['tj_pic'] = $this->input->post('tj_pic');
            $da['pic1'] = $this->input->post('pic1');
            $da['pic2'] = $this->input->post('pic2');
            $da['pic3'] = $this->input->post('pic3');
            $da['price'] = $this->input->post('price');
            $da['price_sc'] = $this->input->post('price_sc');
            $da['color'] = $this->input->post('color');
            $da['color_item'] = $this->input->post('color_item');
            $da['size'] = $this->input->post('size');
            $da['kc'] = $this->input->post('kc');
            $da['dl'] = $this->input->post('dl');
            $da['dl1'] = $this->input->post('dl1');
            $da['dl2'] = $this->input->post('dl2');
            $da['lq'] = $this->input->post('lq');
            $da['bh'] = $this->input->post('bh');
            $da['ml'] = $this->input->post('ml');
            $da['sort'] = $this->input->post('sort');
            $da['ms'] = $this->input->post('ms');
            $da['sj'] = $this->input->post('sj');
            $da['sy'] = $this->input->post('sy');
            $da['nf'] = $this->input->post('nf');

            $da['bq'] = $this->input->post('bq');
            $da['jj'] = $this->input->post('jj');

            $da['xffs'] = $this->input->post('xffs');

            $da['jp'] = 1;
            $da['jp_ktime'] = strtotime($this->input->post('jp_ktime'));
            $da['jp_jtime'] = strtotime($this->input->post('jp_jtime'));
            $da['jp_price'] = $this->input->post('jp_price');
            $da['jp_bl'] = $this->input->post('jp_bl');

            $da['bm_ktime'] = strtotime($this->input->post('bm_ktime'));
            $da['bm_jtime'] = strtotime($this->input->post('bm_jtime'));
            $da['bm_info'] = $this->input->post('bm_info');

            $da['sk_mid'] = $this->input->post('sk_mid');
            $da['sk_je'] = $this->input->post('sk_je');
            $da['sk_is'] = $this->input->post('sk_is');

            $da['lj_numb'] = $this->input->post('lj_numb');
            $da['lj_je'] = $this->input->post('lj_je');

            if ($this->session->userdata('state') == 3) {
                $da['staff_id'] = $this->session->userdata('sid');
            } else {
                $da['shopowner'] = $this->input->post('shopowner');
                $da['staff_id'] = $this->input->post('staff_id');
            }

            $da['gys_id'] = $this->input->post('gys_id');
            $da['gm_bt'] = $this->input->post('gm_bt');

            $da['hb1'] = $this->input->post('hb1');

            $this->pub->insert('sp', $da);
            $dax['idx'] = $this->db->insert_id();
            $dax['msg'] = '添加商品成功！';
            $this->pub->logx('添加商品 - ' . $da['name']);
            echo json_encode(array('type' => 1));
        } else {
            $da = $this->pub->get_setting('商品', 'Commodity');
            $q = $this->db->get_where('sp_brand');
            $da['brand'] = $q->result_array();
            $this->load->view('adminx/sp_add', $da);
        }

    }

    function up($idx)
    {
        $this->pub->logp('修改商品 - ' . $idx, '1-2-3');
        //如果上架了 将不允许修改
        $this->db->select('sj');
        $q = $this->db->get_where('sp', array('sp_id' => $idx));
        $ls = $q->row_array(0);
        if ($ls['sj'] == 1 && $this->session->userdata('state') == 3) {
            redirect('/iadmin/sp');
        }
        if ($_POST) {
            $da['mx']['fl1'] = $this->input->post('fl1');
            $da['mx']['brand_id'] = $this->input->post('brand_id');
            $da['mx']['sort'] = $this->input->post('sort');
            $da['mx']['name'] = $this->input->post('name');

            $da['mx']['tj'] = $this->input->post('tj');
            $da['mx']['tj_pic'] = $this->input->post('tj_pic');
            $da['mx']['pic1'] = $this->input->post('pic1');
            $da['mx']['pic2'] = $this->input->post('pic2');
            $da['mx']['pic3'] = $this->input->post('pic3');
            $da['mx']['price'] = $this->input->post('price');
            $da['mx']['price_sc'] = $this->input->post('price_sc');
            $da['mx']['color'] = $this->input->post('color');
            $da['mx']['color_item'] = $this->input->post('color_item');
            $da['mx']['size'] = $this->input->post('size');
            $da['mx']['kc'] = $this->input->post('kc');
            $da['mx']['dl'] = $this->input->post('dl');

            $da['mx']['dl1'] = $this->input->post('dl1');
            $da['mx']['dl2'] = $this->input->post('dl2');
            $da['mx']['lq'] = $this->input->post('lq');
            $da['mx']['bh'] = $this->input->post('bh');
            $da['mx']['ml'] = $this->input->post('ml');
            $da['mx']['sort'] = $this->input->post('sort');
            $da['mx']['ms'] = $this->input->post('ms');
            $da['mx']['sj'] = $this->input->post('sj');
            $da['mx']['sy'] = $this->input->post('sy');
            $da['mx']['nf'] = $this->input->post('nf');

            $da['mx']['bq'] = $this->input->post('bq');
            $da['mx']['jj'] = $this->input->post('jj');

            $da['mx']['jp'] = 1;
            $da['mx']['jp_ktime'] = strtotime($this->input->post('jp_ktime'));
            $da['mx']['jp_jtime'] = strtotime($this->input->post('jp_jtime'));
            $da['mx']['jp_price'] = $this->input->post('jp_price');
            $da['mx']['jp_bl'] = $this->input->post('jp_bl');

            $da['mx']['bm_ktime'] = strtotime($this->input->post('bm_ktime'));
            $da['mx']['bm_jtime'] = strtotime($this->input->post('bm_jtime'));
            $da['mx']['bm_info'] = $this->input->post('bm_info');

            $da['mx']['sk_mid'] = $this->input->post('sk_mid');
            $da['mx']['sk_je'] = $this->input->post('sk_je');
            $da['mx']['sk_is'] = $this->input->post('sk_is');

            $da['sk_mid']['lj_numb'] = $this->input->post('lj_numb');
            $da['sk_mid']['lj_je'] = $this->input->post('lj_je');

            $da['mx']['xffs'] = $this->input->post('xffs');

            if ($this->session->userdata('state') == 3) {
                $da['mx']['staff_id'] = $this->session->userdata('sid');
            } else {
                $da['mx']['shopowner'] = $this->input->post('shopowner');
                $da['mx']['staff_id'] = $this->input->post('staff_id');
            }

            $da['mx']['gys_id'] = $this->input->post('gys_id');
            $da['mx']['gm_bt'] = $this->input->post('gm_bt');

            if ($this->session->userdata('state') == 3) {
                $this->db->where('staff_id', $this->session->userdata('sid'));
            }

            $da['mx']['hb1'] = $this->input->post('hb1');

            $this->db->update('sp', $da['mx'], array('sp_id' => $idx));
            $this->pub->logx('修改商品 - ' . $da['mx']['name']);
            echo json_encode(array('type' => 1));
        } else {
            $q = $this->db->get_where('sp', array('sp_id' => $idx));
            if ($q->num_rows() > 0) {
                $da = $this->pub->get_setting('商品', 'Commodity');
                $da['mx'] = $q->row_array(0);

                $q = $this->db->get_where('sp_brand');
                $da['brand'] = $q->result_array();
                $this->load->view('adminx/sp_up', $da);
            } else {
                show_error('您的请求不存在！！');
            }
        }
    }

    function del()
    {
        $this->pub->logp('删除商品 - ' . $this->input->post('idx'), '1-2-3');
        $ids = explode(",", $this->input->post('idx'));
        $this->db->where_in('sp_id', $ids);
        $q = $this->db->get_where('sp');
        if ($q->num_rows() > 0) {
            $this->db->where_in('sp_id', $ids);
            //权限控制
            if ($this->session->userdata('state') == 3) {
                $this->db->where('staff_id', $this->session->userdata('sid'));
            }
            $this->db->update('sp', array('sp_del' => 2));
            $da['type'] = 1;
            $this->pub->logx('删除商品 - ' . $this->input->post('idx'));
        } else {
            $da['type'] = 2;
        }
        echo json_encode($da);
    }

    // 上架
    function getup()
    {
        $this->pub->logp('上架商品 - ' . $this->input->post('idx'), '1-2-3');
        if ($this->session->userdata('state') == 3) {
            $this->db->where('staff_id', $this->session->userdata('sid'));
        }
        $ids = explode(",", $this->input->post('idx'));
        $this->db->where_in('sp_id', $ids);
        $q = $this->db->get_where('sp');
        if ($q->num_rows() > 0) {
            $this->db->where_in('sp_id', $ids);
            //权限控制
            if ($this->session->userdata('state') == 3) {
                $this->db->where('staff_id', $this->session->userdata('sid'));
            }
            $this->db->update('sp', array('sj' => 1));
            $da['type'] = 1;
            $this->pub->logx('上架商品 - ' . $this->input->post('idx'));
        } else {
            $da['type'] = 2;
        }
        echo json_encode($da);
    }

    // 下架
    function getdown()
    {
        $this->pub->logp('下架商品 - ' . $this->input->post('idx'), '1-2-3');
        if ($this->session->userdata('state') == 3) {
            $this->db->where('staff_id', $this->session->userdata('sid'));
        }
        $ids = explode(",", $this->input->post('idx'));
        $this->db->where_in('sp_id', $ids);
        $q = $this->db->get_where('sp');
        if ($q->num_rows() > 0) {
            $this->db->where_in('sp_id', $ids);
            //权限控制
            if ($this->session->userdata('state') == 3) {
                $this->db->where('staff_id', $this->session->userdata('sid'));
            }
            $this->db->update('sp', array('sj' => 2));
            $da['type'] = 1;
            $this->pub->logx('下架商品 - ' . $this->input->post('idx'));
        } else {
            $da['type'] = 2;
        }
        echo json_encode($da);
    }

    // 获取商品统计数据
    function get_total()
    {
        $this->pub->logp('获取商品统计数据 - ' . $this->input->post('product_id'), '1-2-3');
        $id = $this->input->post('product_id');
        $da = '';

        $status_q_0 = $this->db->query('SELECT SUM(A.numb) AS numb FROM `pan_bill_item` AS A LEFT JOIN `pan_bill` AS B ON A.bill_id = B.bill_id WHERE B.state = 0 AND A.sp_id = ' . $id);
        $status_q_1 = $this->db->query('SELECT SUM(A.numb) AS numb FROM `pan_bill_item` AS A LEFT JOIN `pan_bill` AS B ON A.bill_id = B.bill_id WHERE B.state = 1 AND A.sp_id = ' . $id);
        $status_q_2 = $this->db->query('SELECT SUM(A.numb) AS numb FROM `pan_bill_item` AS A LEFT JOIN `pan_bill` AS B ON A.bill_id = B.bill_id WHERE B.state = 2 AND A.sp_id = ' . $id);
        $status_q_3 = $this->db->query('SELECT SUM(A.numb) AS numb FROM `pan_bill_item` AS A LEFT JOIN `pan_bill` AS B ON A.bill_id = B.bill_id WHERE B.state = 3 AND A.sp_id = ' . $id);
        $status_q_4 = $this->db->query('SELECT SUM(sp.kc) AS numb FROM `pan_sp` AS sp WHERE sp.sp_id = ' . $id);

        //echo '<p>未付款：' . ($status_q_0->row_array(0)['numb'] == null ? 0 : $status_q_0->row_array(0)['numb']) . '</p>';
        //echo '<p>已付款未发货：' . ($status_q_1->row_array(0)['numb'] == null ? 0 : $status_q_1->row_array(0)['numb']) . '</p>';
       // echo '<p>已收货：' . ($status_q_2->row_array(0)['numb'] == null ? 0 : $status_q_2->row_array(0)['numb']) . '</p>';
       // echo '<p>作废：' . ($status_q_3->row_array(0)['numb'] == null ? 0 : $status_q_3->row_array(0)['numb']) . '</p>';
       // echo '<p>库存：' . ($status_q_4->row_array(0)['numb'] == null ? 0 : $status_q_4->row_array(0)['numb']) . '</p>';
    }

    function get()
    {
        $this->pub->logp('获取商品信息 ', '1-2-3');
        if ($this->session->userdata('state') == 3) {
            $this->db->where('staff_id', $this->session->userdata('sid'));
        }
        if ($this->input->post('brand_id')) {
            $this->db->where('brand_id', $this->input->post('brand_id'));
        }
        if ($this->input->post('key')) {
            $this->db->where('sp_del', 1);
            $this->db->select('count(*)');

            $this->db->like('name', $this->input->post('key'));
            $q = $this->db->get_where('sp');
            $ls = $q->row_array(0);
            $num = $ls['count(*)'];
            $this->db->like('name', $this->input->post('key'));
        } else {
            $this->db->where('sp_del', 1);
            $this->db->select('count(*)');
            $q = $this->db->get_where('sp');
            $ls = $q->row_array(0);
            $num = $ls['count(*)'];
        }
        //一共的页数
        $da['num'] = ceil($num / 30);

        $page = is_numeric($this->input->post('page')) && $this->input->post('page') > 0 ? $this->input->post('page') : 1;
        $page = $page > $da['num'] ? $da['num'] : $page;
        $da['page'] = $page;

        $page = ($page - 1) == -1 ? 0 : ($page - 1);

        $this->db->limit(30, $page * 30);

        //		$this -> db -> order_by('sort', 'asc');
        $this->db->where('sp_del', 1);
        if ($this->session->userdata('state') == 3) {
            $this->db->where('staff_id', $this->session->userdata('sid'));
        }
        if ($this->input->post('brand_id')) {
            $this->db->where('brand_id', $this->input->post('brand_id'));
        }
        $q = $this->db->get_where('sp');
        $da['content'] = '';

        $xffs[1] = '物流快递';
        $xffs[2] = '到店消费';
        $xffs[3] = '无物流商品';
        $xffs[4] = '课程';
        if ($this->session->userdata('state') != 3) {
            $da['content'] .= '<tr><td></td>';
            $da['content'] .= '<td><input type="checkbox" onchange="check_all()"></td>';
            $da['content'] .= '<td></td><td></td><td></td><td></td><td>';
            $da['content'] .= '<button onclick="all_do(\'delete\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button>';
            $da['content'] .= '<button onclick="all_do(\'up\')" type="button" class="btn green"><i class="fa fa-trash-o"></i> 上架</button>';
            $da['content'] .= '<button onclick="all_do(\'down\')" type="button" class="btn blue"><i class="fa fa-trash-o"></i> 下架</button>';
            $da['content'] .= '</td></tr>';

        }
        foreach ($q->result_array() as $v) {
            $this->db->select('name');
            $qq = $this->db->get_where('staff', array('staff_id' => $v['staff_id']));
            $ls = $qq->row_array(0);

            $da['content'] .= '<tr>';
            $da['content'] .= '<td>' . $v['sp_id'] . '</td>';
            $da['content'] .= '<td><input type="checkbox" value="' . $v['sp_id'] . '" class="product-check"/></td>';
            $da['content'] .= '<td ><img width="120" src="' . $this->pub->oss_dz($v['pic1']) . '" /></td>';
            $da['content'] .= '<td><a href="/adminx/yl#/cxt/sp/' . $v['sp_id'] . '"><p>' . $v['name'] . '</p></a><p>市场价格：<span class="badge badge-warning">' . $v['price_sc'] . '</span> 销售价格：<span class="badge badge-primary">' . $v['price'] . '</span> 供货价格：<span class="badge badge-success">' . $v['sk_je'] . '</span></p><p>红包1：<span class="badge badge-warning">' . $v['dl'] . '</span> 红包2：<span class="badge badge-primary">' . $v['dl1'] . '</span> 红包3：<span class="badge badge-success">' . $v['dl2'] . '</span></p><p>供货方式：' . $xffs[$v['xffs']] . ' ' . ($ls['name'] ? $ls['name'] : '') . '  |  栏目：' . $v['fl1'] . '</p></td>';

            $this->db->select('name');
            $qq = $this->db->get_where('sp_brand', array('id' => $v['brand_id']));
            $ls = $qq->row_array(0);
            $da['content'] .= '<td>' . $ls['name'] . '</td>';

            $da['content'] .= '<td>' . ($v['sj'] == 1 ? '上架' : '下架') . '</td>';

            $da['content'] .= '<td>';
            if ($this->session->userdata('state') != 3) {
                $da['content'] .= '<a href="/adminx/yl#/cxt/sp/' . $v['sp_id'] . '" class="btn"><i class="fa fa-cloud"></i>  预览</a> <button onclick="alert_ts(\'商品原链接是：' . site_url('cxt/sp/' . $v['sp_id']) . '\')" type="button" class="btn btn-info"><i class="fa fa-link"></i>链接</button>';
            }
            $da['content'] .= '<button onclick="get_ewm(\'' . $v['sp_id'] . '\',\'' . $v['name'] . '\',2)" type="button" class="btn blue"><i class="fa fa-qrcode"></i> 二维码</button>  ';
            $da['content'] .= '<button onclick="get_total(\'' . $v['sp_id'] . '\')" type="button" class="btn"><i class="fa fa-signal"></i> 统计</button>';
            //上架后 商家不允许修改
            if ($this->session->userdata('state') == 3) {
                if ($v['sj'] != 1) {
                    $da['content'] .= '<a href="/iadmin/sp/up/' . $v['sp_id'] . '" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</a>   ';
                    $da['content'] .= '<button onclick="del(\'' . $v['sp_id'] . '\',\'' . $v['name'] . '\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button>';
                }
            } else {
                $da['content'] .= '<a href="/iadmin/sp/up/' . $v['sp_id'] . '" type="button" class="btn green"><i class="fa fa-edit"></i> 修改</a>   ';
                $da['content'] .= '<button onclick="del(\'' . $v['sp_id'] . '\',\'' . $v['name'] . '\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 删除</button>';
            }
            $da['content'] .= '</td></tr>';
        }
        $da['type'] = 1;
        echo json_encode($da);
    }

}

?>